<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .search-container {
            position: relative;
        }

        #search-box {
            width: 200px;
            transition: width 0.3s ease-in-out;
        }

        #search-box.collapsed {
            width: 0;
            padding: 0;
        }

        #search-button {
            position: absolute;
            right: 0;
        }
    </style>
</head>

<body>
    <div class="search-container">
        <input type="text" id="search-box" placeholder="搜索...">
        <button id="search-button" value="搜索"></button>
    </div>
<script>
    const searchBox = document.getElementById("search-box");

searchBox.addEventListener("focus", function() {
  searchBox.classList.remove("collapsed");
});

searchBox.addEventListener("blur", function() {
  if (searchBox.value === "") {
    searchBox.classList.add("collapsed");
  }
});

</script>
</body>

</html>